package com.dd.ent.car.ui.home.templet.video;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.common.player.MediaPlayerAcceptor;
import com.dd.ent.car.common.player.MediaPlayerManager;
import com.dd.ent.car.common.player.MediaPlayerState;
import com.dd.ent.car.common.player.MediaPlayerStateChangeListener;
import com.dd.ent.car.common.player.ThunderMediaPlayer;
import com.dd.ent.car.common.player.VideoInfo;
import com.dd.ent.car.common.vo.VideoVo;
import com.dd.ent.car.service.video.VideoService;
import com.dd.ent.car.ui.detail.DetailActivity;
import com.dd.ent.car.ui.detail.DetailFragmentImp;
import com.dd.ent.car.ui.home.templet.HomeBottomView;
import com.pp.ent.Cherry;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.Inject;


/**
 * Created by aqu-spring on 16/8/17.
 */
public class VideoFeedVideoView extends FrameLayout implements View.OnClickListener {
    private VideoItemView videoItemView;
    private HomeBottomView homeBottomView;
    private int position;
    private Video video;
    @Inject
    private VideoService videoService;

    public VideoFeedVideoView(Context context) {
        super(context);
        init(context);
    }

    public VideoFeedVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        initResource(context);
        initListeners();
    }

    private void initResource(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_list_video, this, true);
        videoItemView = (VideoItemView) root.findViewById(R.id.video_view);
        homeBottomView = (HomeBottomView) root.findViewById(R.id.video_bottom_view);
    }

    private void initListeners() {
        videoItemView.setOnClickListener(this);
        homeBottomView.setBottomItemViewOnClickListener(new HomeBottomView.BottomItemViewOnClickListener() {
            @Override
            public void onLikeClick(View view) {

            }

            @Override
            public void onCommentClick(View view) {
                Context context = getContext();
                if (context == null) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putInt(DetailFragmentImp.HEADER_VIEW_TYPE, DetailFragmentImp.VIDEO_HEADER);
                bundle.putSerializable(DetailFragmentImp.VIDEO, video);
                DetailActivity.startSelf(context, DetailFragmentImp.VIDEO_HEADER, bundle);
            }

            @Override
            public void onShareClick(View view) {

            }
        });


    }

    public void refreshData(int position, Video item) {
        this.position = position;
        video = item;
        videoItemView.refreshData(position, video);
        homeBottomView.refreshData(position, video);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_view:
                doPlay();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Cherry.injectObject(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Cherry.remove(this);
    }


    private void doPlay() {
        final ThunderMediaPlayer videoPlayer = MediaPlayerManager.getInstance().applyMediaPlayer(getContext(), new MediaPlayerAcceptor() {
            @Override
            public void onPlayerAccept(ThunderMediaPlayer mediaPlayer) {
                mediaPlayer.addContainerTo(videoItemView.getPlayContainer());
                mediaPlayer.addStateChangeListener(mStateChangeListener);
                mediaPlayer.setListPosition(position);
                //videoItemView.startLoading();
            }

            @Override
            public void onPlayerRelease(ThunderMediaPlayer mediaPlayer) {
                mediaPlayer.removeStateChangeListener(mStateChangeListener);
                mediaPlayer.removeSelfContainer();
            }
        }, MediaPlayerManager.PlayerTag.FEED);
        VideoInfo videoInfo = new VideoInfo(video.getUuid(), video.getPlayUrl(), video.getTitle());
        videoPlayer.play(videoInfo);
        sumbitPlayInfo();

    }

    private void sumbitPlayInfo() {
        videoService.sumbitPlayCountToVideo(video.getUuid(), 1);
    }

    private MediaPlayerStateChangeListener mStateChangeListener = new MediaPlayerStateChangeListener() {
        @Override
        public void onStateChange(MediaPlayerState oldState, MediaPlayerState newState) {
            switch (newState) {
                case INITIALIZED:
                case PREPARING:
                    break;
                case STARTED:
                    break;
            }
        }
    };

    @UIHandler(UIConfig.ADD_PLAY_COUNT_TO_VIDEO_SUCCESS)
    public void onAddPlayCountSuccessResponse(VideoVo videoVo) {
        if (videoVo != null && videoVo.result != null && videoVo.result.items != null && !videoVo.result.items.isEmpty()) {
            if (TextUtils.equals(videoVo.result.items.get(0).getUuid(), video.getUuid())) {
                video.setPlayCount(videoVo.result.items.get(0).getPlayCount());
                refreshData(position, video);
            }
        }
    }
}
